package com.msmall.community.show.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.msmall.base.pager.MatchType;
import com.msmall.base.pager.PaginationSupport;
import com.msmall.base.util.ConstantDB;
import com.msmall.community.play.service.PlayManager;
import com.msmall.community.show.model.ShowModel;
import com.msmall.community.show.service.ShowManager;
import com.msmall.community.user.model.UserInfo;
import com.msmall.community.user.service.UserManager;
import com.msmall.community.vote.service.VoteManager;

public class ShowManagerImpl implements ShowManager {

	private static final Log logger = LogFactory.getLog(ShowManagerImpl.class);
	
	private PlayManager playManager;

	private UserManager userManager;
	
	private VoteManager voteManager;

	public void setPlayManager(PlayManager playManager) {
		this.playManager = playManager;
	}
	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	public void setVoteManager(VoteManager voteManager) {
		this.voteManager = voteManager;
	}
	/**
	 * String queryString = "select u.userid,u.username, u.nickname, u.homeUrl,u.gender, u.avatar, u.vipLevel,u.levels,u.birthday, count(v.voted) " +
					" from UserInfo u, Vote v" +
					" where u.userid=v.voted and v.module=" + module +
					" and v.types=" + types +
					" and u.lockStatus=" + ConstantDB.UNLOCK_STATUS +
					" group by(v.voted) order by u.vipLevel DESC, u.levels DESC, u.createTime DESC";
			page.setQueryString(queryString);
			userManager.getPagination(page);
	 * @param page
	 * @throws Exception
	 */
	public void getPagination(PaginationSupport page, Integer module, Integer types){
		try{
			page.setClazz(UserInfo.class);
			page.addFilter("lockStatus", MatchType.EQ, ConstantDB.UNLOCK_STATUS);
			page.setOrderBy("vipLevel,levels,score,createTime");
			page.setOrder("DESC,DESC,DESC,DESC");
			userManager.getPagination(page);
			page.setItems(populate(page.getItems(), module, types));
		}catch(Exception e){
			logger.error(e);
		}
	}
	
	public void getPagination(PaginationSupport page, String nickname, Integer module,Integer types){
		try{
			userManager.getPagination(page, nickname);
			page.setItems(populate(page.getItems(), module, types));
		}catch(Exception e){
			logger.error(e);
		}
	}
	
	public void getPagination(PaginationSupport page, Short gender, Integer agesmax, Integer agesmin, Integer module,Integer types){
		try{
			userManager.getPagination(page, gender, agesmax, agesmin);
			page.setItems(populate(page.getItems(), module, types));
		}catch(Exception e){
			logger.error(e);
		}
	}

	private List<ShowModel> populate(List<UserInfo> items, Integer module, Integer types){
		List<ShowModel> models = new ArrayList<ShowModel>();
		if(items != null && items.size() > 0){
			for(int i=0; i<items.size(); i++){
				UserInfo user = items.get(i);
				ShowModel model = new ShowModel();
				model.setUserid(user.getUserid());
				model.setUsername(user.getUsername());
				model.setNickname(user.getNickname());
				model.setHomeUrl(user.getHomeUrl());
				model.setGender(user.getGender());
				model.setAvatar(user.getAvatar());
				model.setVipLevel(user.getVipLevel());
				model.setLevels(user.getLevels());
				model.setBirthday(user.getBirthday());
				Integer travelVoteCount = 0;
				try{
					travelVoteCount = voteManager.loadTotalVote(user.getUserid(), module, types);
				}catch(Exception e){}
				model.setTravelVoteCount(travelVoteCount);
				models.add(model);
			}
		}
		return models;
	}
}
